********************************************************************************
******"Macroprudential Regulation, Quantitative Easing, and Bank Lending"*******
********************************************************************************

********************************************************************************
********************************************************************************
**********************************2019******************************************
********************************************************************************
********************************************************************************
global y "lncdisbur_w199"
global regcommands ", pool(5) compact absorb(bankid borrowerid#time) vce(cluster bankid)"

clear *
**# Bookmark (input)
import sas ENTE_SEGN CENSITO_CRITTOG TIME DISBUR COM COMUNE_DIP using $inputdirectory/bloans2019, case(lower)
run RO_creditregister tm(2019m2) tm(2020m2) 0
keep bankid borrowerid time disbur comune_dip

**# Bookmark (input)
merge m:1 bankid using $inputdirectory/INB_gru2019, keepus(gru)
keep if _merge!=2
drop _merge
replace gru=bankid if gru==.
drop bankid
rename gru bankid

**# Bookmark (input)
merge m:1 comune_dip using $inputdirectory/INB_comprov2019, keepus(prov)
keep if _merge!=2
drop _merge

collapse (sum) disbur, by(bankid borrowerid time prov)
duplicates tag bankid borrowerid time, g(dup)
egen xxx=max(disbur), by(bankid borrowerid time)
gen yyy=prov if xxx==disbur
egen zzz=max(yyy), by(bankid borrowerid time)
replace prov=zzz if dup>0
drop xxx yyy zzz
collapse (sum) disbur, by(bankid borrowerid time prov)

**# Bookmark (input)
merge m:1 bankid using $inputdirectory/INB_preann2019m8, keepus(dtop66e14 dmid6633e14 exlnassets excashas)
keep if _merge==3
drop _merge

**# Bookmark (input)
merge m:1 bankid time using $inputdirectory/INB_refinm2019, keepus(lncrefin)
keep if _merge!=2
drop _merge

egen id=group(bankid borrowerid)
xtset id time
gen lncdisbur=100*(ln(disbur)-ln(L.disbur))
keep if time>=tm(2019m3)&time<=tm(2020m2)
winsor2 lncdisbur if lncdisbur!=., suffix(_w199) cuts(1 99)

**# Bookmark (input)
merge m:1 bankid prov time using $inputdirectory/INB_provbank2019, keepus(interdis66R interdisno66R)
drop _merge

run RO_tdummies 2019 1
run RO_tdintera 1 "dtop66e14 dmid6633e14 interdis66R interdisno66R exlnassets excashas"

**# Bookmark (output - column 1)
cd $outputdirectory
est clear
reghdfe $y dT7dtop66e14 dT7dmid6633e14 dT7interdis66R dT7interdisno66R dT7exlnassets dT7excashas lncrefin interdis66R interdisno66R, pool(5) absorb(bankid borrowerid#time) vce(cluster prov)
est save TABVB_C12
cd ..

********************************************************************************
********************************************************************************
**********************************2015******************************************
********************************************************************************
********************************************************************************
global y "lncdisbur_w199"
global regcommands ", pool(5) compact absorb(bankid borrowerid#time) vce(cluster bankid)"

clear *
**# Bookmark (input)
import sas ENTE_SEGN CENSITO_CRITTOG TIME DISBUR COM COMUNE_DIP using $inputdirectory/bloans, case(lower)
run RO_creditregister tm(2014m6) tm(2015m6) 0
keep bankid borrowerid time disbur comune_dip 

**# Bookmark (input)
merge m:1 bankid using $inputdirectory/INB_gru2015, keepus(gru)
keep if _merge!=2
drop _merge
replace gru=bankid if gru==.
drop bankid
rename gru bankid

**# Bookmark (input)
merge m:1 comune_dip using $inputdirectory/INB_comprov2015, keepus(prov)
keep if _merge!=2
drop _merge

collapse (sum) disbur, by(bankid borrowerid time prov)
duplicates tag bankid borrowerid time, g(dup)

egen xxx=max(disbur), by(bankid borrowerid time)
gen yyy=prov if xxx==disbur
egen zzz=max(yyy), by(bankid borrowerid time)
replace prov=zzz if dup>0
drop xxx yyy zzz
collapse (sum) disbur, by(bankid borrowerid time prov)

**# Bookmark (input)
merge m:1 bankid using $inputdirectory/INB_preann2014m12, keepus(dtop85e13 exlnassets excashas)
keep if _merge==3
drop _merge

**# Bookmark (input)
merge m:1 bankid time using $inputdirectory/INB_refinm2015, keepus(lncrefin)
keep if _merge!=2
drop _merge

**# Bookmark (input)
merge m:1 bankid prov time using $inputdirectory/INB_provbank2015, keepus(interdis85R interdisno85R)
keep if _merge!=2
drop _merge

egen id=group(bankid borrowerid)
xtset id time
gen lncdisbur=100*(ln(disbur)-ln(L.disbur))
keep if time>=tm(2014m7)&time<=tm(2015m6)
winsor2 lncdisbur if lncdisbur!=., suffix(_w199) cuts(1 99)

recode interdis85R interdisno85R (.=0)

run RO_tdummies 2015 1
run RO_tdintera 1 "dtop85e13 interdis85R interdisno85R exlnassets excashas"

**# Bookmark (output - column 2)
cd $outputdirectory
est clear
reghdfe $y dT7dtop85e13 dT7interdis85R dT7interdisno85R dT7exlnassets dT7excashas lncrefin interdis85R interdisno85R $regcommands
est save TABVB_C12, append
cd ..

